package com.example.lottoscratcho;

import org.andengine.entity.modifier.FadeInModifier;
import org.andengine.entity.modifier.FadeOutModifier;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.BaseGameActivity;

import android.R.integer;
import android.app.Activity;
import android.util.Log;

public class SpotCover {
	
//------------------------------------------------------------------------CONSTANTS:
	
	private static final float ANIMATION_TIME = 10.0f;
	private static final long[] FRAMEDURATIONS = new long[] { 100, 100 };
	private static final int FIRSTTILEINDEX = 10;
	private static final int LASTTILEINDEX = 0;
	private static final boolean LOOP = false;
	
//---------------------------------------------------------------------------FIELDS:
	
	private TiledTextureRegion mTextureRegion;
	private float xPos;
	private float yPos;
	private BaseGameActivity activity;
	public AnimatedSprite cover;
	private Spot thisSpot;
	private MatchCard matchCard;
	private static float opacity = 1;
	private static boolean animationFlag = false;
	
//-----------------------------------------------------------------------CONSTRUCTOR:

	SpotCover(TiledTextureRegion textReg, float xPos, float yPos, BaseGameActivity act, MatchCard matchCard) {
		
		this.mTextureRegion = textReg;
		this.xPos = xPos;
		this.yPos = yPos;
		this.activity = act;
		this.matchCard = matchCard;
		
		createCover();
	
	}

//--------------------------------------------------------------------------METHODS:
	
	private void createCover() {

		//Create new AnimateSprite
		cover = new AnimatedSprite(xPos, yPos, 
								   mTextureRegion, 
								   activity.getVertexBufferObjectManager()) {
			@Override
			protected void onManagedUpdate(float pSecondsElapsed) {
				
				Log.d("TIME", String.valueOf(pSecondsElapsed));
				
				if (animationFlag == true) {
					
					if (opacity > 0) {
						
						// Calculate rotation offset based on time passed
						final float rotationOffset = pSecondsElapsed * 25;
						//set opacity
						opacity -= 0.001;
						this.setAlpha(opacity);
						// Adjust this rectangle's rotation
						this.setRotation(this.getRotation() + rotationOffset);
						// Increase the position by 5 pixels per update
						this.setPosition(this.getX() + 5f, this.getY() + 5f);
					}
					
				}
				
				// Pass the seconds elapsed to our update thread
				super.onManagedUpdate(pSecondsElapsed);
			}
			
			//Set Touch Event
			@Override
			public boolean onAreaTouched(final TouchEvent touchEvent, 
										 final float pTouchAreaLocalX, 
										 final float pTouchAreaLocalY) {

				if (touchEvent.getAction() == TouchEvent.ACTION_UP){
					
					cover.animate((long) ANIMATION_TIME); //Animation starts
					if (animationFlag == false) {
						animationFlag = true; 
					}
					else {
						animationFlag = false;
					}
					 
				}
			
				return true;
			}
		};	
	}
	

	void coverValue() {
		
		cover.animate(FRAMEDURATIONS, FIRSTTILEINDEX, LASTTILEINDEX, LOOP);
	}
	

}
